package com.example.myapplication.Outpatientappointment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;



import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.OutpatientlistBean;
import com.example.myapplication.apter.OutpatientCarouselmapApter;
import com.example.myapplication.apter.OutpatientlistApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OutpatientActivity extends AppCompatActivity {

    private Button outpatient_bloak;
    private SearchView outpatient_sear;
    private Button outpatient_sousu;
    private ViewPager outpatient_vpage;
    private OkHttpClient okHttpClient;
    private  OutpatientlistBean carouselmapBean;
    private Handler handler;
    private RecyclerView outpatient_re;
    private OutpatientlistBean outpatientlistBean;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_outpatient);
        initview();
        //返回
        bingbloak();
        //轮播图
        sendCarouselmap();
        }


    //轮播图
    private void sendCarouselmap() {
        okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/hospital/hospital/list")
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =response.body().string();
                carouselmapBean = gson.fromJson(json, OutpatientlistBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //轮播图适配器
                        bingOutpatientCarouselmap();
                        //列表
                        bingOutpatientlist();
                    }
                });
            }
        });
    }

    //轮播图适配器
    private void bingOutpatientCarouselmap() {
        OutpatientCarouselmapApter outpatientCarouselmapApter = new OutpatientCarouselmapApter(carouselmapBean.getRows(),OutpatientActivity.this);
        outpatient_vpage.setAdapter(outpatientCarouselmapApter);
        mhandler.sendEmptyMessageDelayed(0,3*1000);
    }

    //列表
    private void bingOutpatientlist() {
        OutpatientlistApter outpatientlistApter = new OutpatientlistApter(OutpatientActivity.this,carouselmapBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(OutpatientActivity.this);
        outpatient_re.setAdapter(outpatientlistApter);
        outpatient_re.setLayoutManager(linearLayoutManager);
    }

    private void bingbloak() {
        outpatient_bloak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OutpatientActivity.this.finish();
            }
        });
    }

    private void initview() {
        outpatient_bloak = findViewById(R.id.outpatient_bloak);
        outpatient_sear = findViewById(R.id.outpatient_sear);
        outpatient_sousu = findViewById(R.id.outpatient_sousu);
        outpatient_vpage = findViewById(R.id.outpatient_vpage);
        outpatient_re = findViewById(R.id.outpatient_re);

        handler = new Handler();
        gson = new Gson();
    }

    private Handler mhandler =  new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                int currentItem = outpatient_vpage.getCurrentItem();
                currentItem = (currentItem+1) % 3 ;
                outpatient_vpage.setCurrentItem(currentItem);
                mhandler.sendEmptyMessageDelayed(0,3*1000);
            }
        }
    };
}